/***************************************************************************
 *   Copyright (C) 2007 by Alexandre Masselot,,,   *
 *   alex@alex-laptop   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "peptide.h"
#include <iostream>

using namespace std;
namespace bol{
Peptide::Peptide(const string seq)
{
  fromString(seq);
}


Peptide::Peptide(const Peptide& pept, const int pos, const int len){
  int l=len+1;
  int start=(pos==0)?1:0;
  if(pept.m_sequence[pos+len-1]!=AminoAcid::CHAR_CTERM_PEPTIDE){
    l++;
  }
  m_sequence=new unsigned char[l+1];
  memcpy(m_sequence+1, pept.sequence()+pos+start, len);

  m_sequence[0]=AminoAcid::CHAR_NTERM_PEPTIDE;
  if(pept.m_sequence[pos+len-1]!=AminoAcid::CHAR_CTERM_PEPTIDE){
    m_sequence[l-1]=AminoAcid::CHAR_CTERM_PEPTIDE;
  }
  m_sequence[l]=0;
  m_length=l;
}
void Peptide::fromString(const string& seq){
  int l=seq.size();
  m_sequence=new unsigned char[l+3];

  for (int i=0; i<l; i++){
    m_sequence[i+1]=AminoAcid::aa2reduced(seq.c_str()[i]);
  }
  m_sequence[0]=27;
  m_sequence[l+1]=28;
  m_sequence[l+2]=0;
  m_length=l+2;
}

void Peptide::setSequence(const string& seq){
  if(m_sequence)
     delete[] m_sequence;
  fromString(seq);
}

 Peptide::~Peptide()
{
  if(m_sequence)
    delete[] m_sequence;
}


double Peptide::getMass(unsigned char* seq){
  double m=0;
  for( int i=0; seq[i]; i++){
    m+=AminoAcid::s_char2mass[static_cast<int>(seq[i])];
  }
  return m;
}


}

